<?php
session_start();
require("../php/PasswordHash.php");

// 创建密码哈希实例
$hasher = new PasswordHash(8, false);

// 获取用户提交的用户名和密码
@$username = $_REQUEST["username"];
@$password = $_REQUEST["password"];

// 检查用户名和密码是否已填写
if ($username && $password) {
    // 连接数据库
    $con = mysqli_connect("localhost", "root", "deng123", "housekeeping", "3306") or die("Unable to connect");

    // 反馈用户名是否已存在
    $query = "SELECT * FROM admin_login WHERE username = '$username'";
    $result = mysqli_query($con, $query);

    // 如果用户名不存在，插入新用户
    if (mysqli_num_rows($result) == 0) {
        // 对密码进行哈希加密
        $hashedPassword = $hasher->HashPassword($password);

        // 插入新用户信息
        $insertQuery = "INSERT INTO admin_login (username, password ,card_no) VALUES ('$username', '$hashedPassword','5118789789789')";
        $insertResult = mysqli_query($con, $insertQuery);

        // 检查插入操作是否成功
        if ($insertResult) {
            $_SESSION['username'] = $username;
            header("Location: profile.php"); // 跳转到用户个人资料页面
            exit();
        } else {
            echo "注册失败，请重试。";
        }
    } else {
?>
        <!DOCTYPE html>
        <html lang="en">

        <head>

            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
            <meta name="description" content="">
            <meta name="author" content="">

            <title>好先生 | 家政服务</title>
            <link rel="shortcut icon" href="../img/favicon.ico" />
            <!-- Bootstrap core CSS -->
            <link href="../vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

            <!-- Custom fonts for this template -->
            <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet">
            <link rel="stylesheet" href="../vendor/simple-line-icons/css/simple-line-icons.css">

            <!-- Plugin CSS -->
            <link rel="stylesheet" href="../device-mockups/device-mockups.min.css">

            <!-- Custom styles for this template -->
            <link href="../css/style.css" rel="stylesheet">
            <link href="../css/hover-min.css" rel="stylesheet">
        </head>

        <body id="page-top">

            <!-- Navigation -->
            <nav class="navbar fixed-top bg-info" id="mainNav">
                <div class="container">


                    <div class="col-12">
                        <center><img class="img-rounded" src="../img/logo.png" />&nbsp;<b class="text-warning" style="letter-spacing: 2px;font-size: 25px;border-bottom: 2px solid gainsboro">服务专家</b></center>
                    </div>


                </div>
            </nav>

            <section class="features" id="features" style="background:lavender">
                <div class="container">
                    <div style="text-align: center">
                        <h1 class="mb-5 text-info" style="font-family:Catamaran;font-size: 50px;margin-top: 150px">哎呀！用户或密码不正确.
                            <br>请重新尝试.
                        </h1>
                    </div>
                    <div class="row" style="height: 70px">
                        <div class="col-sm-12 my-auto">
                            <div class="container-fluid">
                                <div class="row">

                                </div>
            </section>
            <footer>
                <div class="container">
                    <p>&copy; 好先生 家政服务. 版权所有.</p>
                    <ul class="list-inline">
                        <li class="list-inline-item">
                            <a href="#">隐私</a>
                        </li>
                        <li class="list-inline-item">
                            <a href="#">条款</a>
                        </li>
                    </ul>
                </div>
            </footer>
            <!-- Bootstrap core JavaScript -->
            <script src="../vendor/jquery/jquery.min.js"></script>
            <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

            <!-- Plugin JavaScript -->
            <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>


            <script src="../js/new-age.min.js"></script>
        </body>

        </html>
<?php
        session_destroy(); //destroy the session
        exit();
    }
} else {
    header("location:index.php");
}

?>